package com.theaf.basemodule.web.controller;

import com.theaf.basemodule.common.api.OrganService;
import com.theaf.basemodule.common.condition.OrganCondition;
import com.theaf.core.constant.DateConstant;
import com.theaf.core.constant.SymbolConstant;
import com.theaf.core.utils.DateUtils;
import com.theaf.core.utils.LoginUtils;
import com.theaf.core.web.ResultHandler;
import com.theaf.basemodule.common.dto.organ.*;
import com.theaf.basemodule.common.vo.organ.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author:songwenke
 * @Description:机构controller
 * @Date:create in 13:34 2018/2/9
 * @Modified By:
 **/
@RestController
@RequestMapping(value="/organ")
@Api(description="机构controller")
public class OrganController {
    @Autowired
    private OrganService organService;

    @Autowired
    private Mapper mapper;

    @RequestMapping(value = "/tree",  method = RequestMethod.POST)
    @ApiOperation(value = "树列表查询", notes = "树列表查询", response = OrganDTO.class)
    public Map tree(@RequestBody @ApiParam(name="查询条件",value="传入json格式",required=true) OrganCondition condition){
        List organRange = LoginUtils.getOrganRange();
        String auths = null;
        if(organRange!=null&&organRange.size()>0){
            auths = SymbolConstant.COMMA;
            for(String r:(List<String>)organRange){
                auths +=r+SymbolConstant.COMMA;
            }
        }
        return ResultHandler.success(organService.findForTree(condition.getName(),
                condition.getType(),condition.getCreater(),condition.getUpdater(),condition.getAreaId(),
                condition.getEnabled(), DateUtils.convertStrToDate(condition.getStart(), DateConstant.YMD),
                DateUtils.convertStrToDate(condition.getEnd(),DateConstant.YMD),
                condition.getPid(),condition.getLevelSize(),auths,false,null));
    }

    @RequestMapping(value = "/saveShop",  method = RequestMethod.POST)
    @ApiOperation(value = "保存门店")
    public Map saveShop(@RequestBody @ApiParam(name="门店数据",value="传入json格式",required=true) ShopVO organVO) throws Exception {
        organService.save(organVO);
        return ResultHandler.success(organVO.getId());
    }

    @RequestMapping(value = "/saveMallSup",  method = RequestMethod.POST)
    @ApiOperation(value = "保存用品供应商")
    public Map saveMallSup(@RequestBody @ApiParam(name="用品供应商数据",value="传入json格式",required=true) MallSupVO mallSupVO)throws Exception{
        organService.save(mallSupVO);
        return ResultHandler.success(mallSupVO.getId());
    }

    @RequestMapping(value = "/saveSup",  method = RequestMethod.POST)
    @ApiOperation(value = "保存配件供应商")
    public Map saveMallSup(@RequestBody @ApiParam(name="配件供应商数据",value="传入json格式",required=true) PartsSupVO partsSupVO) throws Exception {
        organService.save(partsSupVO);
        return ResultHandler.success(partsSupVO.getId());
    }

    @RequestMapping(value = "/saveInsurance",  method = RequestMethod.POST)
    @ApiOperation(value = "保存保险公司")
    public Map saveInsurance(@RequestBody @ApiParam(name="保险公司数据",value="传入json格式",required=true) InsuranceVO insuranceVO) throws Exception {
        organService.save(insuranceVO);
        return ResultHandler.success(insuranceVO.getId());
    }

    @RequestMapping(value = "/saveMemeber",  method = RequestMethod.POST)
    @ApiOperation(value = "保存会员")
    public Map saveMemeber(@RequestBody @ApiParam(name="会员数据",value="传入json格式",required=true) MemberVO memberVO) throws Exception {
        organService.save(memberVO);
        return ResultHandler.success(memberVO.getId());
    }


    @RequestMapping(value = "/getShop/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "查询门店",response = ShopDTO.class)
    @ApiImplicitParam(value = "门店ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map getShop(@PathVariable("id") Long id){
        ShopDTO shopDTO = (ShopDTO)organService.findById(id);
        return ResultHandler.success(shopDTO);
    }

    @RequestMapping(value = "/getMallSup/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "查询用品供应商",response = MallSupDTO.class)
    @ApiImplicitParam(value = "用品供应商ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map getMallSup(@PathVariable("id") Long id){
        MallSupDTO mallSupDTO = (MallSupDTO)organService.findById(id);
        return ResultHandler.success(mallSupDTO);
    }

    @RequestMapping(value = "/getPartsSup/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "查询配件供应商",response = PartsSupDTO.class)
    @ApiImplicitParam(value = "配件供应商ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map getPartsSup(@PathVariable("id") Long id){
        PartsSupDTO partsSupDTO = (PartsSupDTO)organService.findById(id);
        return ResultHandler.success(partsSupDTO);
    }

    @RequestMapping(value = "/getInsurance/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "查询保险公司",response = InsuranceDTO.class)
    @ApiImplicitParam(value = "保险公司ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map getInsurance(@PathVariable("id") Long id){
        InsuranceDTO insuranceDTO = (InsuranceDTO)organService.findById(id);
        return ResultHandler.success(insuranceDTO);
    }

    @RequestMapping(value = "/getMemeber/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "查询会员",response = MemberDTO.class)
    @ApiImplicitParam(value = "会员ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map getMemeber(@PathVariable("id") Long id){
        MemberDTO memberDTO = (MemberDTO)organService.findById(id);
        return ResultHandler.success(memberDTO);
    }

    @RequestMapping(value = "/open/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "开启机构",response = Boolean.class)
    @ApiImplicitParam(value = "机构ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map open(@PathVariable("id") Long id){
        organService.openOrClose(id);
        return ResultHandler.success(true);
    }

    @RequestMapping(value = "/close/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "关闭机构",response = Boolean.class)
    @ApiImplicitParam(value = "机构ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map close(@PathVariable("id") Long id){
        organService.openOrClose(id);
        return ResultHandler.success(true);
    }

    @RequestMapping(value = "/delete/{id}",  method = RequestMethod.GET)
    @ApiOperation(value = "删除机构",response = Boolean.class)
    @ApiImplicitParam(value = "机构ID" , name = "id" , dataType = "long" , paramType = "path")
    public Map delete(@PathVariable("id") Long id){
        organService.delete(id);
        return ResultHandler.success(true);
    }
}
